<?php
/**
 * Created by PhpStorm.
 * User: Liu
 * Date: 2022/5/13
 * Time: 10:31
 */

namespace app\index\controller;


use app\index\Common;

class Solution extends Common {
    public function index(){

        $list = db('solution')
            ->where('is_del',0)
            ->where('status',1)
            ->order('sort','asc')
            ->paginate(6);

        $page = $list->render();

        if (!empty($list)){
            //转换成数据对象,否则会报错
            $list = $list->items();
            foreach ($list as $k => $v){
                $list[$k]['link'] = !empty($v['seo_link']) ? url('index/solution/details',['id'=>$v['seo_link']]) : url('index/solution/details',['id'=>$v['id']]);
                unset($list[$k]['seo_link']);
            }
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        $this->assign('empty','<div align="center">No data</div>');
        return $this->fetch();
    }

    public function details($id){
        $relation_list = [];
        $info = db('solution')
            ->where('id|seo_link',$id)
            ->where('is_del',0)
            ->where('status',1)
            ->find();

        if (!$info){
            //抛出异常
            abort(404, '');
        }

        if (!empty($info['related_products'])) {
            $relation_products = array_filter(explode(',', $info['related_products']));
            $relation_list = db('product')
                ->where('id','in',$relation_products)
                ->field('id,name,cover,describe,seo_link')
                ->select();
            foreach ($relation_list as $k => $v){
                $relation_list[$k]['link'] = !empty($v['seo_link']) ? url('index/product/details',['id'=>$v['seo_link']]) : url('index/product/details',['id'=>$v['id']]);
            }
        }
        $this->assign('relation_list',$relation_list);

        $other = db('solution')
            ->where('is_del',0)
            ->where('status',1)
            ->orderRaw('rand()')
            ->limit(3)
            ->field('id,title,cover')
            ->select();
        $this->assign('other',$other);
        $this->assign('seo_title',$info['seo_title']);
        $this->assign('seo_keywords',$info['seo_keywords']);
        $this->assign('seo_describe',$info['seo_describe']);
        $this->assign('info',$info);
        return $this->fetch();
    }
}